from typing import List, Optional
from pydantic_settings import BaseSettings
from dotenv import load_dotenv

# 确保加载 .env 文件
load_dotenv()


class Settings(BaseSettings):
    # Database
    DATABASE_URL: str = "postgresql+asyncpg://user:password@localhost:5432/fastapi_db"

    # Redis
    REDIS_URL: str = "redis://localhost:6379"

    # JWT
    SECRET_KEY: str = "your-secret-key-here-change-in-production"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 300
    REFRESH_TOKEN_EXPIRE_DAYS: int = 30

    # Application
    DEBUG: bool = True
    API_V1_STR: str = "/api/v1"
    PROJECT_NAME: str = "FastAPI Scaffold"
    VERSION: str = "0.1.0"

    # CORS
    CORS_ORIGINS: List[str] = ["http://49.234.39.152:8091", "http://localhost:8080"]

    # Security
    BCRYPT_ROUNDS: int = 12
    MAX_LOGIN_ATTEMPTS: int = 5

    # AI Model Settings
    OPENAI_API_KEY: str = "your-openai-api-key"
    OPENAI_BASE_URL: Optional[str] = None
    MARKITDOWN_MODEL: str = "gemini-2.5-flash-preview-05-20"
    CHAT_VISION_MODEL: str = "gemini-2.5-flash-preview-05-20"
    CHAT_THINKING_MODEL: str = "deepseek-reasoner"

    # MinIO Settings
    MINIO_ENDPOINT: str = "https://minio.cyydm.shop"
    MINIO_BUCKET_NAME: str = "testbucket"
    MINIO_REGION: str = "local_region"
    MINIO_ACCESS_KEY: str = "admin"
    MINIO_SECRET_KEY: str = "QwerAa@112211"
    MINIO_SECURE: bool = True
    MINIO_UPLOAD_DIR: str = "uploads"  # 默认上传目录

    # Logging Settings
    LOG_LEVEL: str = "INFO"
    LOG_FILE: str = "../logs/app.log"
    LOG_MAX_SIZE: int = 10 * 1024 * 1024  # 10MB
    LOG_BACKUP_COUNT: int = 5
    LOG_ENABLE_CONSOLE: bool = True
    LOG_ENABLE_FILE: bool = True  # 自动检测开发环境，智能启用/禁用文件日志

    # Certificate service
    CERTIFICATE_BASE_URL: Optional[str] = "http://119.29.232.53:16821/certificate"
    CERTIFICATE_CREATE_PATH: str = "/create"
    CERTIFICATE_QUERY_PATH_TEMPLATE: str = "/users/{document_number}/certificates"
    CERTIFICATE_REQUEST_TIMEOUT: float = 10.0

    # Public upload user
    PUBLIC_UPLOAD_USER_PHONE: str = "19900000000"
    PUBLIC_UPLOAD_USER_PASSWORD: str = "Public123!"
    PUBLIC_UPLOAD_USER_FULL_NAME: str = "Public Uploader"

    # Capsule System Settings
    CAPSULE_BASE_URL: str = "http://192.168.1.69:8000"
    CAPSULE_TENANT_ID: str = "a9379b56-2530-4461-8005-9bef9a71861b"
    CAPSULE_ACL_KEY: str = "all"
    CAPSULE_REQUEST_TIMEOUT: float = 30.0

    class Config:
        env_file = ".env"
        case_sensitive = True


# Global settings instance
settings = Settings()
